define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            var table1 = $("#table1");
            console.log(Config);
            // 初始化表格
            table1.bootstrapTable({
                extend: {
                    index_url: 'dispatch/index' + location.search,
                    add_url: 'dispatch/add',
                    edit_url: 'dispatch/edit',
                    del_url: 'dispatch/del',
                    multi_url: 'dispatch/multi',
                    table: 'project',
                },
                url: 'dispatch/index' + location.search,
                pk: 'id',
                sortName: 'id',
                undefinedText:'',
                // search:false,
                showToggle:false,
                // singleSelect:true,
                showExport:false,
                toolbar: "#toolbar1",
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: __('Name'),operate:'like',width:'15%'},
                        {field: 'investment_total', title: __('总投资(亿元)'),operate:'like',width:'10%'},
                        {field: 'settle_street_names', title: __('落户街道'),operate:'like',width:'20%'},
                        {field: 'framework_agreement_time', title: __('签订框架协议时间'),
                            addclass:'datetimerange',
                            operate: 'RANGE',
                            formatter: Table.api.formatter.datetimedate},
                        {field: 'invesment_agreement_time', title: __('签订正式投资协议时间'),
                            addclass:'datetimerange',
                            operate: 'RANGE',
                        formatter: Table.api.formatter.datetimedate},


                        // {field: '', title: __('序号'),operate:false,table: table1,width:'20px',formatter:Table.api.formatter.natureorder},
                        // {field: 'name', title: __('Name')},

                        // {field: 'inspect_time', title: __('Inspect_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,datetimeFormat:'YYYY-MM-DD'},
                        // // {field: 'inspect_time_desc', title: __('Inspect_time'),operate:false},
                        // {field: 'inspect_address', title: __('Inspect_address')},
                        // {field: 'leader_ids', title: __('Leader_ids'),visible:false},
                        // {field: 'leader_names', title: __('Leader_ids'),operate:false},
                        // {field: 'accompany_name', title: __('Accompany_name')},
                        // {field: 'inspect_memo', title: __('Inspect_memo')},
                        // {field: 'chargedep_ids', title: __('Chargedep_ids'),visible:false},
                        // {field: 'chargedep_names', title: __('Chargedep_ids'),operate:false},
                        // {field: 'add_memo', title: __('备注'),operate:false},
                        {field: 'project_source_names', title: __('项目来源'),operate:'like',width:'20%'},

                        {field: 'status',title:'状态',operate:false,
                            visible: Config.isBuDi?false:true,
                            formatter: function (value,row,index) {
                                if(value == 1)
                                {
                                    if(Config.isSuperAdmin)
                                        return '<a href="dispatch/checkedit/ids/'+row.id+'" class="btn btn-danger   btn-dialog btn-xs" title="审核">待审核</a>';
                                    else
                                        return '<a href="javascript:;" class="btn btn-danger   btn-xs" title="审核">待审核</a>';
                                }
                                if(value == 0)
                                    if(Config.isSuperAdmin)
                                        return '<a href="dispatch/checkedit/ids/'+row.id+'" class="btn btn-success  btn-addtabs btn-xs " title="已审核">已审核</a>';
                                    else
                                        return '<a href="javascript:;" class="btn btn-success btn-xs ">已审核</a>';
                                if(value == -1)
                                    return '<a href="dispatch/initedit/ids/'+row.id+'" class="btn btn-danger btn-xs btn-dialog" data-icon="fa fa-pencil" data-title="编辑">待提交</a>';
                            }
                        },
                        {field: 'operate', title: __('Operate'), visible: Config.isBuDi?false:true,table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                rowStyle:function(row,index){
                    var style = {};
                    // var classes =  ["gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
                    if(Config.isSuperAdmin || Config.uid==35)
                    {
                        if(row.is_add == 1 )
                            style={css:{'color':'#ff0000'}};
                    }

                    if(row.reject_memo_status == 1)
                        style={css:{'color':'blue'}};
                    // console.log(style);
                    return style;
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table1);
            Controller.api.bindeventTable(table1);
            var table2 = $("#table2");

            // 初始化表格
            table2.bootstrapTable({
                url: 'dispatch/index' + location.search,
                pk: 'id',
                sortName: 'id',
                toolbar: "#toolbar2",
                queryParams:function(params){
                    // console.log(params);
                    params.filter = JSON.parse(params.filter);
                    params.op = JSON.parse(params.op);
                    params.filter.status = 1;
                    params.op.status = '=';
                    // params.filter.is_important = 0;
                    // params.op.is_important = '=';
                    params.filter = JSON.stringify(params.filter);
                    params.op = JSON.stringify(params.op);
                    return params;
                },
                columns: [
                    [

                        {checkbox: true},
                        {field: 'name', title: __('Name')},
                        {field: 'investment_total', title: __('总投资(亿元)')},
                        {field: 'settle_street_names', title: __('落户街道')},
                        {field: 'chargedep_names', title: __('提报部门')},
                        {field: 'framework_agreement_time_text', title: __('签订框架协议时间')},
                        {field: 'invesment_agreement_time_text', title: __('签订正式投资协议时间')},

                        // {field: 'id', title: __('Id')},
                        // {field: 'name', title: __('Name')},
                        // {field: 'chargedep_ids', title: __('Chargedep_ids'),visible:false},
                        // {field: 'chargedep_names', title: __('Chargedep_ids'),operate:false},
                        {
                            field: 'check',
                            width: "120px",
                            title: __('审核'),
                            table: table2,
                            events: Table.api.events.operate,
                            // visible: Config.isSuperAdmin ? true:false,
                            buttons: [
                                {
                                    name: 'check',
                                    text: '审核',
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-warning  btn-magic btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: 'dispatch/checkedit',
                                }
                            ],
                            formatter: Table.api.formatter.buttons

                        },
                        // {field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                rowStyle:function(row,index){
                    var style = {};
                    if(row.is_add == 1 )
                        style={css:{'color':'#ff0000'}};
                    return style;
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table2);

            var parenttable = table1.closest('.bootstrap-table');
            var options = table1.bootstrapTable('getOptions');
            var toolbar = $(options.toolbar, parenttable);
            $(toolbar).on('click', ".btn-detail", function () {
                var that = this;
                //循环弹出多个编辑框
                $.each(table1.bootstrapTable('getSelections'), function (index, row) {
                    var url = 'approach/detail';
                    row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
                    var url = Table.api.replaceurl(url, row, table1);
                    var icon = 'fa fa-info';
                    Backend.api.addtabs(url, row.name, icon);
                    // Fast.api.open(url, __('详情'), $(that).data() || {});
                });
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        detail: function (){
            $(".btn-print").click(function (){
                var html = $(".print").html();
                var bodyHtml = document.body.innerHTML;
                document.body.innerHTML = html;
                window.print();
                document.body.innerHTML = bodyHtml;
            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        initedit: function () {
            Controller.api.bindevent();
        },
        checkedit:function() {
            $(".all_submit").click(function(event) {
                // console.log($(this).data("item_id"));
                var that = this;
                Layer.confirm(__('确定要通过审核吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        //几个参数需要注意一下
                        type: "POST",//方法类型
                        dataType: "json",//预期服务器返回的数据类型
                        url: "dispatch/checkedit/ids/"+ Config.ids,
                        success: function (ret) {
                            parent.Toastr.success('审核成功');
                            parent.$(".btn-refresh").trigger("click");
                            var index = parent.Layer.getFrameIndex(window.name);
                            parent.Layer.close(index);
                        },
                        error : function(error) {
                            console.log(error);
                        }
                    });
                    Layer.close(index);
                });

            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            bindeventTable: function (table) {
                var parenttable = table.closest('.bootstrap-table');
                //Bootstrap-table配置
                var options = table.bootstrapTable('getOptions');
                // console.log(options);
                //Bootstrap操作区
                var toolbar = $(options.toolbar, parenttable);

                // table.on('dbl-click-row.bs.table', function (e, row, element, field) {
                //     var url = options.extend.edit_url;
                //      row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
                //     var url = Table.api.replaceurl(url, row, table);
                //     var icon = 'fa fa-pencil';
                //     // Backend.api.addtabs(url, '编辑', icon);
                //     // $('.btn-modi', element).trigger("click");
                // });
                // console.log(toolbar);
                // $(".btn-customedit").click(function(event) {
                //     /* Act on the event */alert("aaaa");
                // });
                // alert("aaaa");
                // console.log(toolbar)


                $(toolbar).on('click', ".btn-export", function () {
                    var library_id = $(this).data('library_id');
                    console.log(library_id);
                    var options = table.bootstrapTable('getOptions');
                    var search = options.queryParams({});
                    var filter = search.filter;
                    var op = search.op;
                    // console.log(filter)
                    // console.log(op)
                    if(library_id == 3)
                        url = 'dispatch/export';
                    // console.log(url + '?filter='+filter+'&op='+op);
                    if(url)
                        location.href= url + '?filter='+filter+'&op='+op;
                });


            }
        }
    };
    return Controller;
});